package com.zl.controller;

import com.zl.model.Account;
import com.zl.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    //controller调用service层
    @Autowired
    @Qualifier("AccountServiceIml")
    private AccountService accountService;

    /**
     * 查询
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(Model model){
         List<Account> list= accountService.findAll();
         //返回前端展示
        model.addAttribute("list",list);
        //返回页面
        return "main";
    }

    /**
     * 添加
     * @return
     */
        //1.转到添加页面
        @RequestMapping("/toinsert")
        public  String toinsert(){
            return "add";
        }
        //2.添加的请求
        @RequestMapping("/insert")
        public String insert(Account account){
            //调用service
            accountService.insert(account);
            //重定向到查询全部
            return "redirect:/account/findAll";
        }
    /**
     * 删除
     */
    @RequestMapping("/delete/{accuntid}")
    public String delete(@PathVariable("accuntid") int id){
        accountService.delete(id);
        return "redirect:/account/findAll";
    }

    /**
     * 查一个
     */
    @RequestMapping("/findOne")
    public String findOne(int id,Model model){
        Account account=accountService.findOne(id);
        model.addAttribute("account",account);
        return "update";
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public String update(Account account){
        accountService.update(account);
        return "redirect:/account/findAll";
    }
}
